package edu.sjsu.itack.bl;



import java.util.Properties;  
import edu.sjsu.itack.util.ApplicationProperties;


import javax.mail.*;  

import javax.mail.internet.InternetAddress;  

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;  

import javax.mail.internet.MimeMultipart;

  

public class Mailer {  

public static void send(String to,String subject,String msg){  

	final String user = ApplicationProperties.getProperty("emailID");
	final String pass = ApplicationProperties.getProperty("emailPass");
Properties props = new Properties();  
props.put("mail.smtp.host", "smtp.gmail.com");//change accordingly
props.put("mail.smtp.auth", "true");  
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.EnableSSL.enable","true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");   
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getDefaultInstance(props,  
 new javax.mail.Authenticator() {  
  protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(user,pass);  
   }  

});  
try {  
 MimeMessage message = new MimeMessage(session);  
 message.setFrom(new InternetAddress(user));  
 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
 message.setSubject(subject); 
 MimeMultipart content = new MimeMultipart();
 MimeBodyPart text = new MimeBodyPart();
 MimeBodyPart html = new MimeBodyPart();
 //3rd step)send message  
 String htmlMsg = "<!DOCTYPE html><html lang=\"en\"><head><link href=\"css/home.css\" rel=\"stylesheet\"></head><body style=\"background-color:white\"><div class=\"imgclass\"><img border=\"0\" src=\"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRGSDorX-jcqK-qfMrDq9hFH13Pe-cRRSQLn2YL-QMY9sHR3f0SKQ\" alt=\"iTack\" width=\"800\" height=\"228\" ></div><h1><font color=\"Orange\">iTack!! </font></h1><h2><font color=\"Black\">Gather the things you love at one place!!!</font></h2><h3><font color=\"Black\">Please click <a href=\"http://localhost:8080/itack/signup.jsp\">here</a> to join!</font></h3><h4>"+msg+"</h4></body></html>";
 
 message.setContent(htmlMsg, "text/html; charset=utf-8");
//message.setText(msg);
 Transport.send(message);  

  

 System.out.println("mail sent!");  

  

 } catch (MessagingException e) {  

    throw new RuntimeException(e);  

 }  

      

}  

}  